﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SDL
{
    public class ConnectionString : ICloneable
    {
        private static readonly Regex _ProviderRegEx = new Regex(@"(?i:(Provider)=(?<provider>[^;]+))", RegexOptions.Compiled);
        private static readonly Regex _ServerRegEx = new Regex(@"(?i:((Data\sSource)|(Server)|(Address)|(Addr)|(Network Address))=(?<server>[^;]+))", RegexOptions.Compiled);
        private static readonly Regex _AuthRegEx = new Regex(@"(?i:((Integrated\sSecurity)|(Trusted_Connection))=(?<auth>(true)|(yes)|(sspi)))", RegexOptions.Compiled);
        private static readonly Regex _DatabaseRegEx = new Regex(@"(?i:((Initial\sCatalog)|(Database))=(?<database>[^;]+))", RegexOptions.Compiled);
        private static readonly Regex _PasswordRegEx = new Regex(@"(?i:((Password)|(Pwd))=(?<password>[^;]+))", RegexOptions.Compiled);
        private static readonly Regex _UserRegEx = new Regex(@"(?i:((User\sID)|(UID)=(?<user>[^;]+)))", RegexOptions.Compiled);
        private static readonly Regex _CacheRegEx = new Regex(@"(?i:((Cached)|(Cache)=(?<cache>[^;]+)))", RegexOptions.Compiled);

        public override string ToString()
        {
            if (string.IsNullOrEmpty(Server) && string.IsNullOrEmpty(Database))
            {
                return ("");
            }

            var connectionString = string.Empty;
            if (!string.IsNullOrEmpty(Server))
            {
                connectionString += "Server=" + Server + ";";
            }

            if (!string.IsNullOrEmpty(Database))
            {
                connectionString += "Database=" + Database + ";";
            }

            if (Authentication)
            {
                connectionString += "Integrated Security=SSPI;";
            }

            if (!string.IsNullOrEmpty(UserID))
            {
                connectionString += "User ID=" + UserID + ";";
            }

            if (!string.IsNullOrEmpty(Password))
            {
                connectionString += "Pwd=" + Password + ";";
            }
            return (connectionString);
        }

        public static ConnectionString Parse(string sStr)
        {
            var oStr = new ConnectionString
            {
                Provider = _ProviderRegEx.Match(sStr).Groups["provider"].Value,
                Server = _ServerRegEx.Match(sStr).Groups["server"].Value,
                Database = _DatabaseRegEx.Match(sStr).Groups["database"].Value,
                UserID = _UserRegEx.Match(sStr).Groups["user"].Value,
                Cached = !string.IsNullOrEmpty(_CacheRegEx.Match(sStr).Groups["cache"].Value),
                Password = _PasswordRegEx.Match(sStr).Groups["password"].Value
            };
            oStr.Authentication = (
                (oStr.Database.Length == 0) ||
                (_AuthRegEx.Match(sStr).Groups["auth"].Value.Length > 0)
                                    );
            return (oStr);
        }

        public string Provider { get; set; }

        public string Server { get; set; }

        public string Database { get; set; }

        public string UserID { get; set; }

        public string Password { get; set; }

        public bool Authentication { get; set; }

        public bool Cached { get; set; }

        #region ICloneable Members
        public object Clone()
        {
            var clone = new ConnectionString
            {
                Authentication = Authentication,
                Database = Database,
                Password = Password,
                Provider = Provider,
                Server = Server,
                UserID = UserID
            };
            return (clone);
        }
        #endregion
    }
}
